package com.lktx.sso.scope;

import cn.dev33.satoken.oauth2.SaOAuth2Manager;
import cn.dev33.satoken.oauth2.data.model.AccessTokenModel;
import cn.dev33.satoken.oauth2.data.model.ClientTokenModel;
import cn.dev33.satoken.oauth2.scope.handler.SaOAuth2ScopeHandlerInterface;
import cn.dev33.satoken.secure.SaSecureUtil;
import cn.hserver.core.ioc.annotation.Bean;

import java.util.LinkedHashMap;
import java.util.Map;

@Bean
public class OpenIdScopeHandler implements SaOAuth2ScopeHandlerInterface {

    @Override
    public String getHandlerScope() {
        return "openid";
    }

    private String getOpenid(String clientId, Object loginId) {
        return SaSecureUtil.md5(SaOAuth2Manager.getServerConfig().getOpenidDigestPrefix() + "_" + clientId + "_" + loginId);
    }

    @Override
    public void workAccessToken(AccessTokenModel at) {
        Map<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("openid", getOpenid(at.getClientId(), at.getLoginId()));
        at.extraData.putAll(map);
    }

    @Override
    public void workClientToken(ClientTokenModel ct) {
    }

}